<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Wikey.Entity.Page>" %>

<script type="text/javascript">
  function SaveComment() {
    $.post('<%= Html.ResolveUrl("~/Page/Comment/")%>',
                {
                  PageId: $('#PageId').val(),
                  From: $('#From').val(),
                  Comment: $('#CommentText').val()
                },
                function(response) {

                }
            );
  }
  var CurrentPageId = '<%= ViewData.Model.Id %>';

  $(function() {
    var btnUpload = $('#attach');
    var status = $('#status');
    new AjaxUpload(btnUpload, {
      action: ('<%= Html.ResolveUrl("~/Page/Upload/")%>' + CurrentPageId),
      //Name of the file input box  
      name: 'uploadfile',
      onSubmit: function(file, ext) {
        if (!(ext && /^(jpg|png|jpeg|gif|doc|xls|xlsx|docx|txt|pdf|zip|rar)$/.test(ext))) {
          // check for valid file extension  
          status.text('Only JPG, PNG or GIF files are allowed');
          return false;
        }
        status.text('Uploading...');
      },
      onComplete: function(file, response) {
        //On completion clear the status  
        status.text('');
        jsonResponse = response.replace("<pre>", "").replace("</pre>", "");

        jsonObj = eval('(' + jsonResponse + ')');

        try {
          for (var i = 0; i < jsonObj.length; i++) {
            $('<li></li>').appendTo('#files').html('<img src="./Uploads/' + file + '" alt="" /><br />' + file).addClass('success');
            $("#PageDocuments").val($("#PageDocuments").val() + "|" + jsonObj[i].Id);

          }
        } catch (e) {
          $('<li></li>').appendTo('#files').text(file).addClass('error');
        }
      }
    });
    new PageEditor('PageContent', CurrentPageId);
    
  });
  
  var TextAreaTemplate = '<div id="content-{0}"> <textarea id="area-{0}"> </textarea> </div>';
  function AddContent() {
    uniqueId = UniqueId();
    
    html = String.format ( TextAreaTemplate , uniqueId );
    textAreaId = "#area-"+uniqueId;
    
    $('#PageContent').append(html);
    mySettings.onSave = function(markItUp) {
      parameters = { Page: CurrentPageId, data: markItUp.textarea.value };
      $.post('<%= Html.ResolveUrl("~/Page/Content/0")%>', parameters, function(response) {
        postResponse = eval("(" + response + ")");
      });

    };
    
    $(textAreaId).markItUp(mySettings);

  }      
  function UniqueId() {
        var uniqueId = Math.random().toString().replace(".", "").replace(",", "");
        return uniqueId;
    }

    $(document).ready(
      function() {
        $("#PageAttachmentsLink").bind('click',
          function() { $('#PageAttachments').toggle(); }
          );
      }
    );
      function HidePageAttachments() {
      $('#PageAttachments').css({display:"none"});
      }     
        
    </script>